package pl.prv.polanie.client.widgets.table;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dialogs.CreateDeclarationDialog;
import pl.prv.polanie.client.dto.CSDeclarationDTO;
import pl.prv.polanie.client.dto.CSVDeclarationDTO;
import pl.prv.polanie.client.dto.ICSVDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.panels.lists.DeclarationsList;
import pl.prv.polanie.client.widgets.table.subwidgets.TableTextBox;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Janusz Prokulewicz
 * Created: Jun 10, 2008
 */
public class TableDeclarations extends Composite {
	
	DeclarationsList parentDeclarationsList = null;
	private long groupID;
	
	private CustomTable mainCT = new CustomTable(){
		
		@Override
		protected void runOnClickSelectedRow(int row, int column, ICSVDTO csvDto, Widget w) {
			super.runOnClickSelectedRow(row, column, csvDto, w);

			CSDeclarationDTO csDto = (CSDeclarationDTO)mainCT.getCSDtoByID(csvDto.getId());
			
			CreateDeclarationDialog cdd = new CreateDeclarationDialog(groupID, parentDeclarationsList, csDto); 
			cdd.center();
		}
		
		@Override
		protected void runOnSelectRow(ICSVDTO csvDto) {
			super.runOnSelectRow(csvDto);
			
			
//			Schedule schedule = getParentSchedule();
//			if (schedule != null) {
//				schedule.setSelectedDeclarationByID(csvDto.getId());
//			}
			
			Planner.log(".runOnSelectRow: " + "SELECTED ROW");
			if (parentDeclarationsList != null) {
				Planner.log(".runOnSelectRow: " + "WYSZLO");
				parentDeclarationsList.showRemoveSelectedRecordButton();
				parentDeclarationsList.showAddSubjectEventsBasingOnSelectedDeclaration();
			}
//			TableDeclarations.this.runOnHighlightingRow(csvDto);
			
			Planner.log(".runOnSelectRow: " + "wybrano rekord " + csvDto);
		}
		
		
//		@Override
		protected void runOnDoubleClickOnSelectedRow(int row, int column, ICSVDTO csvDto, Widget w) {
			super.runOnDoubleClickOnSelectedRow(row, column, csvDto, w);
			if (column == CSVDeclarationDTO.SORT_CONST__SUBJECT) {
				Label label = (Label)w;				
				
				TableTextBox tb = new TableTextBox(label, mainCT, row, column);
				//tb.setText(label.getText());
				
//				int left = label.getAbsoluteLeft();
//				int top = label.getAbsoluteTop();
//				
//				RootPanel.get().add(tb, left, top);
				
				int width = label.getOffsetWidth();
				int height = label.getOffsetHeight() - 2;
				
				
				mainCT.setWidget(row, column, tb);
//				mainCT.setVerticalAlignment(row, column, HasVerticalAlignment.ALIGN_TOP);
				tb.setWidth(width + "px");
				tb.setHeight(height + "px");
				
				tb.setFocus(true);
				tb.setCursorPosAtEnd();
			}
		}
		
		@Override
		protected void runOnDoubleClickOnNotSelectedRow(int row, int column, ICSVDTO csvDto, Widget w) {
			super.runOnDoubleClickOnNotSelectedRow(row, column, csvDto, w);
		}
		
		
		

		
	};
	
	public TableDeclarations(long groupID, DeclarationsList subjectEventsList) {
		this.groupID = groupID;
		
		parentDeclarationsList = subjectEventsList;
		
		mainCT.setColumnCount(4);
		initWidget(mainCT);
		
		mainCT.addHeaderColumn("subject", 450);
		mainCT.addHeaderColumn("subgroup type", 150);
//		mainCT.addHeaderColumn("subgroups amount", 70);
		mainCT.addHeaderColumn("duration", "duration of events to schedule for every subgroup given type", 100);
		mainCT.addHeaderColumn("to schedule", "how many hours left to schedule", 100);
//		mainCT.addHeaderColumn(" ", 20);
//		loadData();
	}
	
//	public void loadData() {
//		for (int i = 0; i < 40; i++) {
//			long no = i;
//			long no2 = no % 10; 
//			CSDeclarationDTO cs = new CSDeclarationDTO(no, 1, (long)2, (long)2, (long)1, (long)1, (long)1, new Integer(660), new Integer(100));
//			CSVDeclarationDTO csv = new CSVDeclarationDTO(no, 2, "subject " + (no + no2), "room " + no, "subgroup " + no, "lect " + no,
//					"poniedzialek", new Integer(660), new Integer(100),
//					"subjectName-name", "subjectName-symbol", "subjectType-name", "subjectType-initial",
//					"lecturerName", "lecturerSurname", "lecturerAT"
//					);
//			
//			mainCT.addRow(cs, csv);
//		}
//	}
	
	
	/**
	 * Dodaje do tabeli
	 * @param cs
	 * @param csv
	 */
	public void addRow(CSDeclarationDTO cs, CSVDeclarationDTO csv) {
		mainCT.addRow(cs, csv);
	}
	
	
	public void saveOrUpdateDeclarationInTable(CSDeclarationDTO csDto) {
		CSVDeclarationDTO csvSE = ViewDataProvider.getCSVDeclaration(csDto);
		mainCT.addOrUpdateDTOInTable(csDto, csvSE);
	}
	
	
	
	/**
	 * Wysyla calla do serwera usuwajacego na bazie declaration i jesli operacja sie uda to usuwa takze z tabelki.
	 * @param csDto
	 */
	public void removeSelectedDeclarationFromTable() {

		final CSVDeclarationDTO csvSE = (CSVDeclarationDTO)mainCT.getSelectedCSVDto();
		if (csvSE != null) {
			final CSDeclarationDTO csSE = (CSDeclarationDTO)mainCT.getCSDtoByID(csvSE.getId());
			
			
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					Planner.log("TableDeclarations.removeSelectedDeclarationFromTable() FAILURE");
				}
				public void onSuccess(Boolean res) {
					Planner.log("TableLecturers.removeSelectedDeclarationFromTable() SUCCESS -> zwrocone wynik = " + res);

					
					//TODO dodac do kontrolki tabelki
					if (res.booleanValue() == true) {
						if (mainCT != null) {
							mainCT.removeRow(csvSE);
						}
					} else {
						
					}

					//update w cache
					Cache.removeCSDeclaration(csSE);
//					parentSchedule.removeCSDeclaration(csSE);
					parentDeclarationsList.hideRemoveSelectedRecordButton();
					
					
				}
			};
			
			ServiceHolder.get().removeCSDeclaration(csSE, callback);
		}
		
	}	
	
	
	
	protected void runOnHighlightingRow(ICSVDTO csvDto) {}

	public void highlightRowByID(long id) {
		mainCT.highlightRowByID(id);
	}
	
	public void setSelectedRowByID(long id) {
		mainCT.setSelectedRowByID(id);
	}
	
	public void removeSelectionFromRow() {
		mainCT.removeSelectionFromSelectedRow();
	}
	
	public void removeHighlightingFromRows() {
		mainCT.removeHighlightingFromRows();
	}

	public CSDeclarationDTO getSelectedCSDto() {
		return mainCT.getSelectedCSDto();
	}
	
	
	
}
